package qiurank;

import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2015/12/31.
 */
public class Common {
    /**
     * MD5加密
     * @param string 要加密的字符串
     * @return   加密后的字符串
     */
    public static String stringToMD5(String string){
        byte[] hash;
        try {
            hash= MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        StringBuilder hex=new StringBuilder(hash.length*2);
        for(byte b:hash){
            if((b&0xFF)<0x10){
                hex.append("0");
            }
            hex.append(Integer.toHexString(b&0xFF));
        }
        return hex.toString();
    }
    public static String unicode(String m){
        char[] bt=m.toCharArray();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<bt.length;i++){
            String hb=Integer.toHexString(bt[i]);
            if(hb.length()<=2){
                hb="00"+hb;
            }
            sb.append("\\u"+hb);
        }
        return sb.toString();
    }

    /**
     * 或得当前时间往前推几个月
     * @param pre_month  要往前推的月份
     * @return  获得推几个月后的年-月
     */
    public static String getNowDateToPreMonth(int pre_month){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy_MM");
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH,pre_month);
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获得当天时间00:00:00的时间戳
     * @return
     */
    public static long getNowDateNingChen(){
        return getAnyDateNingChen(0);
    }

    /**
     *  获得任意时间的00:00:00的时间戳，距离现在
     * @param diff  相对于现在时刻的距离（天）
     * @return java时间戳
     */
    public static long getAnyDateNingChen(int diff){
        Calendar t=Calendar.getInstance();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        t.add(Calendar.DATE,diff);
        String _today=t.get(Calendar.YEAR)+"-"+(t.get(Calendar.MONTH)+1)+"-"+t.get(Calendar.DATE)+" "+"00:00:00";
        try {
            return df.parse(_today).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    /**
     * 把字符串的时分秒转化成java时间戳
     * @param time  字符串的时分秒字符串
     * @return java时间戳
     */
    public static long strToDate(String time){
        Calendar t=Calendar.getInstance();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String rtime=t.get(Calendar.YEAR)+"-"+(t.get(Calendar.MONTH)+1)+"-"+t.get(Calendar.DATE)+" "+time;
        try {
            return df.parse(rtime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;

    }

    /**
     * 获得当前时间 以格式yyyy-mm-dd HH:mm:ss
     * @return
     */
    public static String getNowDate(){
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }
}
